|
|
|
|
|
|
|
Figure C.9
Using GROUP BY creates a summary record for each defined group. |
|
|
|
|
|
|
|
|
The GROUP BY clause can also include an optional HAVING clause. The HAVING clause works similarly to a WHERE clause but examines only the field values of the returned records. The HAVING clause determines which of the selected records to display; the WHERE clause determines which records to select from the base tables. You can use the HAVING clause to display only those salespeople with total sales exceeding $3,000 for the month. Listing C.16 shows this example; Figure C.10 shows the output from this listing. |
|
|
|
|
|
|
|
|
Listing C.16 HAVING.TXTThe HAVING Clause Filters the Display of the Selected Group Records |
|
|
|
|
|
|
|
|
SELECT SL.SalesID, Min(SL.Quantity * RT.Retail) AS Minsls, _
Max(SL.Quantity * RT.Retail) AS Maxsls, _
Avg(SL.Quantity * RT.Retail) AS Avgsls, _
Sum(SL.Quantity * RT.Retail) AS Totsls, _
Sum(SL.Quantity) AS Totvol _
FROM Sales AS SL, [Retail Items] AS RT _
SL INNER JOIN RT ON SL.[Item Code]=RT.[Item Code] _
GROUP BY SL.SalesID _
HAVING Sum(SL.Quantity * RT.Retail) > 3000 |
|
|
|
|
|
|
|
|
Figure C.10
The HAVING clause limits the display of group records. |
|
|
|
|
|